﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DeliveryManage.Controllers
{
    public class BaseController : Controller
    {
        #region 管理员============================================
        /// <summary>
        /// 判断管理员是否已经登录(解决Session超时问题)
        /// </summary>
        public bool IsAdminLogin()
        {
            //如果Session为Null
            if (Session["session_admin_info"] != null)
            {
                return true;
            }
            else
            {
                //检查Cookies
                string adminname = Utils.GetCookie("AdminName", "DeliveryManage");
                string adminpwd = Utils.GetCookie("AdminPwd", "DeliveryManage");
                if (adminname != "" && adminpwd != "")
                {
                    DeliveryManage.DAL.Sys_User dal = new DeliveryManage.DAL.Sys_User();
                    //把明文进行加密重新赋值
                    adminpwd = ZyEncrypter.GetMd5str(adminname + adminpwd);
                    DeliveryManage.Model.Sys_User model = dal.GetModel(adminname, adminpwd);
                    if (model != null)
                    {
                        model.NewOrderId = 0;
                        Session["session_admin_info"] = model;
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 取得管理员信息
        /// </summary>
        public DeliveryManage.Model.Sys_User GetAdminInfo()
        {
            if (IsAdminLogin())
            {
                DeliveryManage.Model.Sys_User model = Session["session_admin_info"] as DeliveryManage.Model.Sys_User;
                if (model != null)
                {
                    return model;
                }
            }
            return null;
        }

        /// <summary>
        /// 检查管理员权限
        /// </summary>
        /// <param name="nav_name">菜单名称</param>
        /// <param name="action_type">操作类型</param>
        public void ChkAdminLevel(string nav_name, string action_type)
        {
            DeliveryManage.Model.Sys_User model = GetAdminInfo();
            if (model == null)
            {
                return;
            }
            DeliveryManage.DAL.Sys_Role dal = new DeliveryManage.DAL.Sys_Role();
            bool result = false; 
            DeliveryManage.Model.Sys_Role rolemodel = dal.GetModel(model.RoleType);
            if (rolemodel != null)
            {
                if (rolemodel.role_type == 1)
                {
                    result = true;
                    return;
                }
                DeliveryManage.Model.Sys_RoleValue modelt = rolemodel.user_role_values.Find(p => p.nav_name == nav_name && p.action_type.Contains(action_type));
                if (modelt != null)
                {
                    result = true;
                    return;
                }
            }
            if (!result)
            {
                string msgbox = "alert(\"您没有管理该页面的权限，请勿非法进入！\")";
                //HttpContext.Response.Write("<script type=\"text/javascript\">layui.use(['form', 'layer'],function() {$ = layui.jquery;var form = layui.form,layer = layui.layer;}); " + msgbox + " </script>");
                HttpContext.Response.Write("<script type=\"text/javascript\">"+msgbox+"</script>");
                HttpContext.Response.End();
                return;
            }
        }

        /// <summary>
        /// 检查管理员权限
        /// </summary>
        /// <param name="nav_name">菜单名称</param>
        /// <param name="action_type">操作类型</param>
        public bool ChkAdminLevels(string nav_name, string action_type)
        {
            DeliveryManage.Model.Sys_User model = GetAdminInfo();
            if (model == null)
            {
                return false;
            }
            DeliveryManage.DAL.Sys_Role dal = new DeliveryManage.DAL.Sys_Role();
            bool result = false; 
            DeliveryManage.Model.Sys_Role rolemodel = dal.GetModel(model.RoleType);
            if (rolemodel != null)
            {
                if (rolemodel.role_type == 1)
                {
                    return  true;
                }
                DeliveryManage.Model.Sys_RoleValue modelt = rolemodel.user_role_values.Find(p => p.nav_name == nav_name && p.action_type.Contains(action_type));
                if (modelt != null)
                {
                    return true;
                }
            }
            return result;
        }
        
        #endregion
        #region JS提示============================================
        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        protected void JscriptMsg(string msgtitle, string url)
        {
            string msbox = "layer.msg(\"" + msgtitle + "\")";
            //ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
        /// <summary>
        /// 带回传函数的添加编辑删除提示(失败时)
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="callback">JS回调函数</param>
        protected void JscriptMsg(string msgtitle, string url, string callback)
        {
            string msbox = "layui.use(['form', 'layer'],function() {$ = layui.jquery;var form = layui.form,layer = layui.layer;layer.msg(\"" + msgtitle + "\", {icon: 5},function() {layer.close();});});";//xadmin.father_reload();
            //ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
        /// <summary>
        /// 带回传函数的添加编辑删除提示(成功时)
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="callback">JS回调函数</param>
        protected void JscriptMsgShow(string msgtitle, string callback)
        {
            string msbox = "layui.use(['form', 'layer'],function() {$ = layui.jquery;var form = layui.form,layer = layui.layer;layer.alert(\"" + msgtitle + "\", {icon: 6},function() {layer.close();xadmin.father_reload();});});";
            //ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
        #endregion

        #region 获取是否添加或修改============================================
        public string GetAddOrEdit(string action)
        {
            if (action=="Add")
            {
               return action = "Add";
            }
            return "Edit";
        }
        #endregion
    }
}